home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / Tools & Goodies / IntlTest / Sources / Content.cpp < prev    next >
Encoding:
Text File  |  1996-09-12  |  11.7 KB  |  403 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Content.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "IntlTest.hpp"
  11.  
  12. #ifndef CONTENT_H
  13. #include "Content.h"
  14. #endif
  15.  
  16. #ifndef PART_H
  17. #include "Part.h"
  18. #endif
  19.  
  20. #ifndef BINDING_K
  21. #include "Binding.k"
  22. #endif
  23.  
  24. // --- Framework Includes ---
  25.  
  26. #ifndef FWEDVIEW_H
  27. #include "FWEdView.h"
  28. #endif
  29.  
  30. #ifndef FWKIND_H
  31. #include "FWKind.h"
  32. #endif
  33.  
  34. // ----- Foundation Includes -----
  35.  
  36. #ifndef FWSTREAM_H
  37. #include "FWStream.h"
  38. #endif
  39.  
  40. #ifndef FWSUSINK_H
  41. #include "FWSUSink.h"
  42. #endif
  43.  
  44. // --- OpenDoc Includes ---
  45.  
  46. // for ODSetITextProp
  47. #ifndef _STDTYPIO_
  48. #include "StdTypIO.h"
  49. #endif
  50.  
  51. // ----- Macintosh Includes -----
  52.  
  53. #ifndef __SCRIPT__
  54. #include <Script.h>
  55. #endif
  56.  
  57. //========================================================================================
  58. //    Runtime info
  59. //========================================================================================
  60.  
  61. #ifdef FW_BUILD_MAC
  62. #pragma segment odfIntlTest
  63. #endif
  64.  
  65. //========================================================================================
  66. //    Global Functions
  67. //========================================================================================
  68.  
  69. //----------------------------------------------------------------------------------------
  70. //    InternalizeIntlText
  71. //----------------------------------------------------------------------------------------
  72. FW_Boolean InternalizeIntlText(Environment* ev, ODStorageUnit* storageUnit, FW_CString& textData)
  73. {
  74.     FW_Boolean result = false;
  75.     ODIText* iText = ODGetITextProp(ev, storageUnit, kODPropContents, kODMacIText, NULL);
  76.     if (iText)
  77.     {
  78.         textData.ReplaceAll(iText);
  79.         /* Do we need to free the iText? */
  80.         result = true;
  81.     }
  82.  
  83.     return result;
  84. }
  85.  
  86. //----------------------------------------------------------------------------------------
  87. //    InternalizeText
  88. //----------------------------------------------------------------------------------------
  89. FW_Boolean InternalizeText(Environment* ev, ODStorageUnit* storageUnit, FW_CString& textData)
  90. {
  91.     FW_Boolean result = false;
  92.  
  93. #ifdef FW_BUILD_MAC
  94.     unsigned long textSize = storageUnit->GetSize(ev);
  95.     if (textSize > 0)
  96.     {
  97.         char buffer[256];
  98.         if (textSize > 255) 
  99.             textSize = 255;
  100.         FW_CByteArray byteArray;
  101.         storageUnit->GetValue(ev, textSize, byteArray);
  102.         byteArray.CopyBuffer(&buffer, textSize);
  103.         
  104.         textData = "";
  105.         textData.Append((const FW_Char*) &buffer, textSize);
  106.         
  107.         result = TRUE;
  108.     }
  109. #endif
  110.  
  111.     return result;
  112. }
  113.  
  114. //========================================================================================
  115. //    CIntlTestContent class
  116. //========================================================================================
  117.  
  118. FW_DEFINE_AUTO(CIntlTestContent)
  119.  
  120. //----------------------------------------------------------------------------------------
  121. // CIntlTestContent constructor
  122. //----------------------------------------------------------------------------------------
  123. CIntlTestContent::CIntlTestContent(Environment* ev, CIntlTestPart* part) :
  124.     FW_CContent(ev, part),
  125.     fEnglishText(""),
  126.     fJapaneseText("", gJapaneseLocale),
  127.     fIntlTestPart(part)
  128. {
  129. }
  130.  
  131. //----------------------------------------------------------------------------------------
  132. // CIntlTestContent destructor
  133. //----------------------------------------------------------------------------------------
  134.  
  135. CIntlTestContent::~CIntlTestContent()
  136. {
  137. }
  138.  
  139. //----------------------------------------------------------------------------------------
  140. // CIntlTestContent::GetTextData
  141. //----------------------------------------------------------------------------------------
  142.  
  143. const FW_CString& CIntlTestContent::GetTextData(ODID whichView)
  144. {
  145.     if (whichView == kJapaneseEditView)
  146.         return fJapaneseText;
  147.  
  148.     return fEnglishText;
  149. }
  150.  
  151. //----------------------------------------------------------------------------------------
  152. // CIntlTestContent::SetTextData
  153. //----------------------------------------------------------------------------------------
  154.  
  155. void CIntlTestContent::SetTextData(    Environment*, 
  156.                                     const FW_CString& newText, 
  157.                                     ODID whichView)
  158. {
  159.     if (whichView == kJapaneseEditView)
  160.         fJapaneseText = newText;
  161.     else
  162.         fEnglishText = newText;
  163. }
  164.  
  165. //----------------------------------------------------------------------------------------
  166. // CIntlTestContent::Externalize
  167. //----------------------------------------------------------------------------------------
  168.  
  169. void CIntlTestContent::ExternalizeKind(Environment* ev,
  170.                                     ODStorageUnit* storageUnit,
  171.                                     FW_CKind* kind,
  172.                                     FW_StorageKinds storageKind,
  173.                                     FW_CPromise* promise,
  174.                                     FW_CCloneInfo* cloneInfo)
  175. {
  176. FW_UNUSED(storageKind);
  177. FW_UNUSED(promise);
  178. FW_UNUSED(cloneInfo);
  179.  
  180.     if (kind->IsPartKind(ev)) 
  181.     {
  182.         // Update the strings from the edit views, if we have any
  183.         fIntlTestPart->UpdateTextData(ev);
  184.         ExternalizePartKind(ev, storageUnit, kind->GetType(ev));
  185.     }
  186.     else if (kind->IsEqual(ev, kODMacIText))
  187.     {
  188.         // Write the main string as an ODIText
  189.         ODSetITextProp(ev, storageUnit, kODPropContents, kODMacIText, fEnglishText.RevealODIText());
  190.     }
  191.     else if (kind->IsEqual(ev, kTEXTOSType, kODPlatformDataType))
  192.     {
  193.         // Write the main string in Mac 'TEXT' format
  194.         FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, kind->GetType(ev));
  195.         FW_CWritableStream stream(suSink);
  196.         stream.Write(fEnglishText.RevealBuffer(), fEnglishText.GetByteLength());
  197.     }
  198. }
  199.  
  200. //----------------------------------------------------------------------------------------
  201. // CIntlTestContent::ExternalizePartKind
  202. //----------------------------------------------------------------------------------------
  203.  
  204. void CIntlTestContent::ExternalizePartKind(Environment* ev, ODStorageUnit* storageUnit,
  205.                                            ODType partKind)
  206. {
  207.     // Write the three text data strings to storage
  208.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, partKind);
  209.     FW_CWritableStream archive(suSink);
  210.  
  211.     unsigned long stringCount = 2;
  212.     archive << stringCount;
  213.  
  214.     unsigned long whichString = kEnglishEditView;
  215.     archive << whichString;
  216.     archive << fEnglishText;
  217.  
  218.     whichString = kJapaneseEditView;
  219.     archive << whichString;
  220.     archive << fJapaneseText;
  221. }
  222.  
  223. //----------------------------------------------------------------------------------------
  224. // CIntlTestContent::InternalizeKind
  225. //----------------------------------------------------------------------------------------
  226.  
  227. FW_Boolean CIntlTestContent::InternalizeKind(Environment* ev,
  228.                                     ODStorageUnit* sourceSU, 
  229.                                      FW_CKind* kind,
  230.                                     FW_StorageKinds storageKind,
  231.                                     FW_CCloneInfo* cloneInfo)
  232. {
  233. FW_UNUSED(storageKind);
  234. FW_UNUSED(cloneInfo);
  235.  
  236.     FW_Boolean internalized = FALSE;
  237.  
  238.     if (kind->IsPartKind(ev))
  239.     {
  240.         internalized = InternalizePartKind(ev, sourceSU, kind->GetType(ev));
  241.     }
  242.     else if (kind->IsEqual(ev, kODMacIText))
  243.     {
  244.         internalized = InternalizeIntlText(ev, sourceSU, fEnglishText);
  245.     }
  246.     else if (kind->IsEqual(ev, kTEXTOSType, kODPlatformDataType))
  247.     {
  248.         internalized = InternalizeText(ev, sourceSU, fEnglishText);
  249.     }
  250.  
  251.     return internalized;
  252. }
  253.  
  254. //----------------------------------------------------------------------------------------
  255. //    CIntlTestContent::InternalizePartKind
  256. //----------------------------------------------------------------------------------------
  257.  
  258. FW_Boolean CIntlTestContent::InternalizePartKind(Environment* ev, ODStorageUnit* storageUnit,
  259.                                                  ODType partKind)
  260. {
  261.     FW_Boolean internalized = TRUE;
  262.  
  263.     if (storageUnit->GetSize(ev) != 0)
  264.     {
  265.         // Read the text data strings from storage
  266.         FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, partKind);
  267.         FW_PBufferedSink sink(ev, suSink);
  268.         FW_CReadableStream archive(sink);
  269.  
  270.         unsigned long stringCount, whichString;
  271.         archive >> stringCount;
  272.         for (unsigned long i = 0; i < stringCount; i++)
  273.         {
  274.             archive >> whichString;
  275.             if (whichString == kJapaneseEditView)
  276.                 archive >> fJapaneseText;
  277.             else
  278.                 archive >> fEnglishText;
  279.         }
  280.     }
  281.     
  282.     return internalized;
  283. }
  284.  
  285. //========================================================================================
  286. //    CIntlTestSelectedContent class
  287. //========================================================================================
  288.  
  289. FW_DEFINE_AUTO(CIntlTestSelectedContent)
  290.  
  291. //----------------------------------------------------------------------------------------
  292. // CIntlTestSelectedContent constructor
  293. //----------------------------------------------------------------------------------------
  294. CIntlTestSelectedContent::CIntlTestSelectedContent(Environment* ev, CIntlTestPart* part) :
  295.     FW_CContent(ev, part),
  296.     fIntlTestPart(part),
  297.     fEditView(NULL)
  298. {
  299. }
  300.  
  301. //----------------------------------------------------------------------------------------
  302. // CIntlTestSelectedContent destructor
  303. //----------------------------------------------------------------------------------------
  304.  
  305. CIntlTestSelectedContent::~CIntlTestSelectedContent()
  306. {
  307. }
  308.  
  309. //----------------------------------------------------------------------------------------
  310. // CIntlTestSelectedContent::ExternalizeKind
  311. //----------------------------------------------------------------------------------------
  312.  
  313. void CIntlTestSelectedContent::ExternalizeKind(Environment* ev,
  314.                                     ODStorageUnit* storageUnit,
  315.                                     FW_CKind* kind,
  316.                                     FW_StorageKinds storageKind,
  317.                                     FW_CPromise* promise,
  318.                                     FW_CCloneInfo* cloneInfo)
  319. {
  320. FW_UNUSED(storageKind);
  321. FW_UNUSED(promise);
  322. FW_UNUSED(cloneInfo);
  323.  
  324.     FW_CString selectedText;
  325.     GetSelectedText(ev, selectedText);
  326.  
  327.     if (kind->IsEqual(ev, kODMacIText))
  328.     {
  329.         // Write the selected string as an ODIText
  330.         ODSetITextProp(ev, storageUnit, kODPropContents, kODMacIText, selectedText.RevealODIText());
  331.     }
  332.     else if (kind->IsEqual(ev, kTEXTOSType, kODPlatformDataType))
  333.     {
  334.         // Write the selected string in Mac 'TEXT' format
  335.         FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, kind->GetType(ev));
  336.         FW_CWritableStream stream(suSink);
  337.         stream.Write(selectedText.RevealBuffer(), selectedText.GetByteLength());
  338.     }
  339. }
  340.  
  341. //----------------------------------------------------------------------------------------
  342. // CIntlTestSelectedContent::InternalizeKind
  343. //----------------------------------------------------------------------------------------
  344.  
  345. FW_Boolean CIntlTestSelectedContent::InternalizeKind(Environment* ev,
  346.                                         ODStorageUnit* sourceSU, 
  347.                                          FW_CKind* kind,
  348.                                         FW_StorageKinds storageKind,
  349.                                         FW_CCloneInfo* cloneInfo)
  350. {
  351. FW_UNUSED(storageKind);
  352. FW_UNUSED(cloneInfo);
  353.     FW_Boolean internalized = false;
  354.     FW_CString selectedText;
  355.  
  356.     if (kind->IsEqual(ev, kODMacIText))
  357.     {
  358.         internalized = InternalizeIntlText(ev, sourceSU, selectedText);
  359.     }
  360.     else if (kind->IsEqual(ev, kTEXTOSType, kODPlatformDataType))
  361.     {
  362.         internalized = InternalizeText(ev, sourceSU, selectedText);
  363.     }
  364.  
  365.     if (internalized)
  366.     {
  367.         fInternalizedText = selectedText;
  368.         this->SetSelectedText(ev, selectedText);
  369.     }
  370.  
  371.     return internalized;
  372. }
  373.  
  374. //----------------------------------------------------------------------------------------
  375. void CIntlTestSelectedContent::GetSelectedText(Environment* ev, FW_CString& text)
  376. {
  377.     if (fEditView == NULL)
  378.         text = "";
  379.     else
  380.         text = fEditView->GetSelectedText(ev);
  381. }
  382.  
  383. //----------------------------------------------------------------------------------------
  384. void CIntlTestSelectedContent::SetSelectedText(Environment* ev, const FW_CString& newText)
  385. {
  386.     FW_ASSERT(fEditView);
  387.     fEditView->SetSelectedText(ev, newText);
  388. }
  389.  
  390. //----------------------------------------------------------------------------------------
  391. void CIntlTestSelectedContent::ClearSelectedText(Environment* ev)
  392. {
  393.     FW_ASSERT(fEditView);
  394.     fEditView->SetSelectedText(ev, FW_CString());
  395. }
  396.  
  397. //----------------------------------------------------------------------------------------
  398. void CIntlTestSelectedContent::SetSelectionRange(Environment* ev, short startOffset, short endOffset)
  399. {
  400.     if (fEditView)
  401.         fEditView->SelectText(ev, startOffset, endOffset);
  402. }
  403.